var asfman = {};
asfman.Capture = function(obj){
    if(obj.setCapture)
    {
         return {start: function (obj){obj.setCapture();}, end: function (obj){obj.releaseCapture();}};
    }else if(window.captureEvents)
    {
         return {start: function(obj){window.captureEvents(Event.MouseMove||Event.MouseUp);}, end: function(obj){window.releaseEvents(Event.MouseMove||Event.MouseUp);}};
    }
    return null;
}  
var jpDataGrid={
     selIndex:-1,
	 selTdIndex:-1,
	 cobj:{},
	  datagrid:function(){
		 var This=this;
 			 //grid 托动	
			// alert(fwidth);
			var grid = {
				headers: [],
				cols: [],
				dragStart: function(i,x) {	
				    if(i==0){this.resizing=false;return false;}
					this.resizing = { idx: i, startX: x};
				   
				},
				dragMove: function(x) {	
					 
					if (this.resizing) {
						var diff = x-this.resizing.startX
						var h = this.headers[this.resizing.idx]	
						var newWidth = parseInt(h.width) + diff	
							 

						if (newWidth > 60) { 
							h.el.style.width = newWidth+"px";
							h.newWidth = newWidth;
							}
					}
				},
				dragEnd: function() {				
					if (this.resizing) {
						var idx = this.resizing.idx
						this.headers[idx].width = this.headers[idx].newWidth						
						this.cols[this.resizing.idx].style.width = this.headers[idx].newWidth+"px";
						this.resizing = false; 				
						}					 
				},
				scroll: function() {
					var v=$1("divMain").scrollLeft;
					 $("#divHead").scrollLeft(v);
				 
				 }
			}

		// var Myem=new $M.Element()
		 var thead=$("#dgHead th");
		 try{
		 document.getElementById("divHead").onselectstart=function(){return false;}
		 }catch(e){};
		 var count = 0;
	     var tdn=thead.length-1;        
		 $("#dgHead th").each(  			 
			function (i,n){			 
			   if(count==tdn){return false;}
			   	var w=(count==0)?30:fwidth[count-1];	
				var res = document.createElement("span");               
				res.innerHTML="&nbsp;";				 
				var idx=count;				 
				$(res).bind("mousedown",function (e) {	
					     var o = (e.target || e.srcElement);	
						var b=asfman.Capture(o);
						b.start(o)                      
						grid.dragStart(idx,e.clientX);
						return false;
				})
					
                $(this).width(w) ;	
				// $(this).css("bgcolor","red") ;	
				$(this).prepend(res);
				 
				grid.headers[count++] = { width: w, el: this };
						
			}) 	
		count = 0;			 
		$("tbody tr:first td","#dgMain").each(
			function() {			
				if(count==tdn){return false;}
				var w=(count==0)?30:fwidth[count-1];					 
				$(this).width(w);
				grid.cols[count++] = this ;
			}
		);
		 				 
		var rs = $("tbody tr","#dgMain").size() ;
		 
		$("#dgMain").bind("mousedown",	
				function(e){	
					 var _objTd = (e.target || e.srcElement);
					 if(_objTd.tagName!='TD') return false;					 
					_objTr=_objTd.parentNode;
			        This.cobj.bgColor="white";					
					 _objTr.bgColor='#1075e7';
					This.cobj=_objTr
					if(_objTr.rowIndex==-1) return false;					   
					 This.selIndex=_objTr.rowIndex>rs-1?-1:_objTr.rowIndex					 
					DataViewclick()
				}
		   ).bind("mouseover",
				function(e) {
					var td = (e.target || e.srcElement);
					 //td.className = "hover" 					
					 //td.parentNode.className = "hover"
				}
			).bind("mouseout",
				function(e) {
					var td = (e.target || e.srcElement);
					 //td.className = "fTD" 
					 //td.parentNode.className = "fTD"
				}

			)	
		 $("#divMain").bind("scroll",function (e) {grid.scroll()})
		 $("#dgHead").bind("mousemove",function (e) { grid.dragMove(e.clientX); })  
		 
		 $(document).bind("mouseup",function (e) {			 
			
			 var   a=document;	
			 var o = (e.target || e.srcElement);	
			 var b=asfman.Capture(o);
			 b.end(o)
			 grid.dragEnd();
			  
		 })		 
		$("#divHead").css({  width: '100%',height:"25px",  overflow: "hidden"})			
		$("#divMain").bind("dblclick",function(e){	
			var o = (e.target || e.srcElement);			 
			if(o.tagName!='TD') return false;	
			 DataViewdbclick();	
		});
		
	 } 		 
}
